package com.qianniu.napi.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qianniu.napi.admin.entity.Fenxiao;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.mapper.FenxiaoMapper;
import com.qianniu.napi.admin.service.IFenxiaoService;
import com.qianniu.napi.admin.service.IUserService;
import com.qianniu.napi.admin.web.AccountController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 分销设置表 服务实现类
 * </p>
 *
 * @author chenxuebin
 * @since 2019-01-16
 */
@Service
public class FenxiaoServiceImpl extends ServiceImpl<FenxiaoMapper, Fenxiao> implements IFenxiaoService {

    private static final Logger logger = LoggerFactory.getLogger(FenxiaoServiceImpl.class);

    @Autowired
    private IUserService userService;
    @Autowired
    private FenxiaoMapper fenxiaoMapper;

    public BigDecimal getTxfei(User user){
        logger.info("----------------------------getTxfei user="+ JSON.toJSONString(user));

        Fenxiao fx = new Fenxiao();
        fx.setHytype(user.getUtype());
        fx.setMpid(user.getMpid());
        Fenxiao fenxiao = fenxiaoMapper.selectOne(fx);
        logger.info("----------------------------getTxfei fenxiao="+ JSON.toJSONString(fenxiao));
        if(fenxiao==null){
            logger.error("----------------------------fenxiao is null for fx="+JSON.toJSONString(fx));
            return null;
        }
        if(user.getMemberLevel()==1){
            return fenxiao.getTxfei1();
        }else if(user.getMemberLevel()==2){
            return fenxiao.getTxfei2();
        }else if(user.getMemberLevel()==3){
            return fenxiao.getTxfei3();
        }else if(user.getMemberLevel()==4){
            return fenxiao.getTxfei4();
        }else if(user.getMemberLevel()==5){
            return fenxiao.getTxfei5();
        }else if(user.getMemberLevel()==6){
            return fenxiao.getTxfei6();
        }else if(user.getMemberLevel()==7){
            return fenxiao.getTxfei7();
        }

        return null;
    }

    public BigDecimal getMzhekou(User user){
        logger.info("----------------------------getTxfei user="+ JSON.toJSONString(user));

        Fenxiao fx = new Fenxiao();
        fx.setHytype(user.getUtype());
        fx.setMpid(user.getMpid());
        Fenxiao fenxiao = fenxiaoMapper.selectOne(fx);
        logger.info("----------------------------getTxfei fenxiao="+ JSON.toJSONString(fenxiao));
        if(fenxiao==null){
            logger.error("----------------------------fenxiao is null for fx="+JSON.toJSONString(fx));
            return null;
        }

        BigDecimal bai = new BigDecimal(100);
        if(user.getMemberLevel()==1&&fenxiao.getMzhekou1()!=null){
            return fenxiao.getMzhekou1().divide(bai);
        }
        if(user.getMemberLevel()==2&&fenxiao.getMzhekou2()!=null){
            return fenxiao.getMzhekou2().divide(bai);
        }
        if(user.getMemberLevel()==3&&fenxiao.getMzhekou3()!=null){
            return fenxiao.getMzhekou3().divide(bai);
        }
        if(user.getMemberLevel()==4&&fenxiao.getMzhekou4()!=null){
            return fenxiao.getMzhekou4().divide(bai);
        }
        if(user.getMemberLevel()==5&&fenxiao.getMzhekou5()!=null){
            return fenxiao.getMzhekou5().divide(bai);
        }
        if(user.getMemberLevel()==6&&fenxiao.getMzhekou6()!=null){
            return fenxiao.getMzhekou6().divide(bai);
        }
        if(user.getMemberLevel()==7&&fenxiao.getMzhekou7()!=null){
            return fenxiao.getMzhekou7().divide(bai);
        }

        return null;
    }
	
}
